﻿using AdTech.Common;

namespace AdTech.Snap
{
    /// <summary>
    /// The heart of the application.
    /// </summary>
    public class Bootstrapper
    {
        // Properties
        /// <summary>
        /// Gets the command line arguments for this process.
        /// </summary>
        public string[] Args { get; set; }
        /// <summary>
        /// Gets an instance of <see cref="AdTech.Common.CommandLineArgs"/> 
        /// associated with the command line arguments for this process.
        /// </summary>
        public CommandLineArgs Arguments { get; protected set; }
        /// <summary>
        /// Gets the model of the main view.
        /// </summary>
        public MainViewModel MainView { get; protected set; }
        /// <summary>
        /// Gets the configuration instance.
        /// </summary>
        public IConfiguration Configuration { get; protected set; }
        /// <summary>
        /// Gets the logging instance.
        /// </summary>
        public ILogger Logger { get; protected set; }
        /// <summary>
        /// Gets a value whether this process was started in debug mode.
        /// </summary>
        public static bool Debug { get; private set; }

        // Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="AdTech.Snap.Bootstrapper"/> class.
        /// </summary>
        public Bootstrapper(string[] args)
        {
            this.Args = args;
        }

        // Public Methods
        /// <summary>
        /// Starts the current bootstrapper.
        /// </summary>
        public void Run()
        {
            // Init logger
            this.Logger = EventLogLogger.Create();
            Log.Create(this.Logger);

            this.Arguments = new CommandLineArgs(this.Args);
            this.Arguments.Parse();

            // Load config
            this.Configuration = DefaultConfiguration.Load();

            // Load models
            ISnapFactory snap = SnapFactory.Create(this.Configuration);
            IDropboxFactory dropbox = DropboxFactory.Create(this.Configuration.DropboxUID, this.Configuration.DefaultFileDestination);
            IQRCodeFactory qrcode = QRCodeFactory.Create();

            MainWindow wnd = new MainWindow();
            this.MainView = new MainViewModel(this.Configuration, snap, dropbox, qrcode);
            this.MainView.RequestClose = () => wnd.Close();
            wnd.DataContext = this.MainView;

            if (this.Arguments.GetBool("crop").Equals(true))
                this.MainView.NewScreenshotCommand.Execute(null);

            if (this.Arguments.GetBool("debug").Equals(true))
                Debug = true;
        }
    }
}
